package com.site.wisdom_construction.controller.publicModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.publicModule.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2021/12/29
 * @Description
 */
@Api(tags = "班组信息接口")
@RestController
@RequestMapping("Team")
@CrossOrigin
public class TeamController {
    @Autowired
    private TeamService teamService;

    @ApiOperation(value = "增加班组信息", notes = "测试数据:")
    @PostMapping("/insertTeam")
    public JsonResult insertTeam(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = teamService.insertTeam(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入班组信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入班组信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除班组信息", notes = "测试数据:")
    @PostMapping("/deleteTeam")
    public JsonResult deleteTeam(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = teamService.deleteTeam(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除班组信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除班组信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改班组信息", notes = "测试数据:")
    @PostMapping("/updateTeam")
    public JsonResult updateTeam(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = teamService.updateTeam(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改班组信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改班组信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询班组信息", notes = "测试数据:")
    @PostMapping("/findTeamByPage")
    public Page findTeamByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(teamService.Total(map));
        page.setItems(teamService.findTeamByPage(map));
        return page;
    }

    @ApiOperation(value = "下拉框查询班组信息", notes = "测试数据:")
    @PostMapping("/selectTeam")
    public OneDataResult selectTeam(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(teamService.selectTeam(map));
        return oneDataResult;
    }

    @ApiOperation(value = "批量导入班组信息", notes = "测试数据:")
    @PostMapping("/batchInsertTeam")
    public void batchInsertProfession(@RequestBody Map map){
        List list = (List) map.get("data");
        for (int i =0;i<list.size();i++){
            Map mapR = new HashMap();
            JSONObject json=(JSONObject) JSONObject.toJSON(list.get(i));
            mapR.put("teamName",json.get("name"));
            teamService.insertTeam(mapR);
        }
    }
}
